# {
#     "funcname": {
#         "offset": count
#     }
# }
profiles = {}

lines = []
with open("/data/pmudata.log", "r") as f:
    lines = f.readlines()

#   comm   pid   tid cpu period    evt count         addr symbolName                    offset               module fileName lineNum
# mysqld 20451 29007   0     61 cycles     0 ffffa4802884 __pthread_mutex_init            0x44 /usr/lib64/libc.so.6 
# mysqld 20451 29007   0  11537 cycles     0       d1acf8 plugin_thdvar_init(THD*, bool) 0x148 /home/wrf/install/mysql-8.0.25_Wlq/bin/mysqld 
def process_single_line(line):
    try:
        data = line.split(" ")
        comm, pid, tid, cpu, period, evt, count, addr = data[:8]
        symbolName = " ".join(data[8:-3])
        offset, module = data[-3:-1]
        return symbolName, offset, module
    except:
        print("parsing failed: " + line)
        return "error", "0"

def update_profile(symbolName, offset):
    if symbolName in profiles:
        if offset in profiles[symbolName]:
            profiles[symbolName][offset] += 1
        else:
            profiles[symbolName][offset] = 1
    else:
        profiles[symbolName] = {}

for line in lines:
    if line.strip() == "":
        continue
    symbolName, offset, module = process_single_line(line)
    if module != "/home/wrf/install/mysql-8.0.25_Wlq/bin/mysqld":
        continue
    update_profile(symbolName, offset)

# print(profiles)

print("profile len: ", len(profiles))
for func in profiles.keys():
    for offset in profiles[func].keys():
        print("func: ", func, " offset: ", offset, " count: ", profiles[func][offset])

